/////////////////////////////////////////////////////////////////////////////
//
// XepdtRosterListener.cpp
//
//
/////////////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "XepdtRosterListener.h"
#include "XepdtClient.h"

#include "CodeLib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CXepdtRosterListener

/////////////////////////////////////////////////////////////////////////////
// CXepdtRosterListener constructor and destructor
#define STATUS_OFFLINE	"Offline"
#define STATUS_IDLE		"Idle"
#define STATUS_TESTING	"Testing"
#define STATUS_ABNORMAL	"Abnormal"


CXepdtRosterListener::CXepdtRosterListener( CXepdtClient* pXepdtClient )
{
	ASSERT( pXepdtClient != NULL );
	m_pClient = pXepdtClient;
}

CXepdtRosterListener::~CXepdtRosterListener()
{
}

/////////////////////////////////////////////////////////////////////////////
// CXepdtRosterListener method implementation

void CXepdtRosterListener::handleItemAdded( const JID& jid )
{
	m_pClient->rosterManager()->subscribe(jid);
}

void CXepdtRosterListener::handleItemSubscribed( const JID& jid )
{
	m_pClient->rosterManager()->subscribe(jid);
}

void CXepdtRosterListener::handleItemRemoved( const JID& jid )
{
}

void CXepdtRosterListener::handleItemUpdated( const JID& jid )
{
}

void CXepdtRosterListener::handleItemUnsubscribed( const JID& jid )
{
}

void CXepdtRosterListener::handleRoster( const Roster& roster )
{
	Roster::const_iterator it;
	for(it = roster.begin(); it != roster.end(); it++)
	{
		mdt_member member;
		member.jid = (*it).first;
		member.name = (*it).second->name();
		if ((*it).second->online())
		{
			member.status = eMS_Idle;
		}
		else
		{
			member.status = eMS_Offline;
		}
		m_Roster.push_back(member);
	}
	if (m_fGetClients)
		m_fGetClients(m_Roster);
}

void CXepdtRosterListener::handleRosterPresence( const RosterItem& item, const std::string& resource,
												Presence::PresenceType presence, const std::string& msg )
{
	vmdt_members::iterator it;
	for(it = m_Roster.begin(); it != m_Roster.end(); it++)
	{
		if ((*it).jid == item.jid())
		{
			if(presence == Presence::Available)
			{
				if (msg == STATUS_IDLE)
				{
					(*it).status = eMS_Idle;
				}
				else if (msg == STATUS_TESTING)
				{
					(*it).status = eMS_Testing;
				}
				else if (msg == STATUS_ABNORMAL)
				{
					(*it).status = eMS_Abnormal;
				}
			}
			else if(presence == Presence::Unavailable)
			{
				(*it).status = eMS_Offline;
			}
		}
	}
	if (m_fGetClients)
		m_fGetClients(m_Roster);
}


void CXepdtRosterListener::handleSelfPresence( const RosterItem& item, const std::string& resource,
											  Presence::PresenceType presence, const std::string& msg )
{
}

bool CXepdtRosterListener::handleSubscriptionRequest( const JID& jid, const std::string& msg )
{
	return true;
}

bool CXepdtRosterListener::handleUnsubscriptionRequest( const JID& jid, const std::string& msg )
{
	return true;
}

void CXepdtRosterListener::handleNonrosterPresence( const Presence& presence )
{
	m_pClient->requestRoster();
}

void CXepdtRosterListener::handleRosterError( const IQ& iq )
{
}